<?php

require_once('Helper.php');

class Common extends Helper{

    
	function Common(){
		parent::Helper();
		$this->ci()->load->helper('url');
		$this->ci()->load->helper('cookie');
		$this->ci()->load->library('helpers/Html_tag_helper', '', 'tag');
		$this->ci()->load->library('session');
		
	}
	
	
	
	public function slide_show($ci_ss,$res){
		
		$images = base_url().'uploads/'; 
		$size = count($res);
		
		$pics = array();
		
		if($size>0){
			unset($pics);
			for ($i = 0; $i < $size; $i++){
				$pics[] = base_url().$res[$i]['sFileName'];
				
			}
		}
		
		$ci_ss->speed(3000);
		$ci_ss->width('651px');
		$ci_ss->height('315px');
		
		$ci_ss->pictures($pics);
		$ci_ss->jqPath('');
		if($size>0){
			return $ci_ss->renderSlideShow();	
		}else{
			return '';
		}
		
	}
	
	function user($index=null){
		$re_ = $this->ci()->session->userdata('user');
		$ret = $re_;
		if($index){
			$ret = $re_[$index];	
		}
		return $ret;
	}
	
	private function  min($pid){
		$attr = " style=\"cursor:pointer\" class=\"what\" param=\"$pid\" id=\"has_$pid\" state=\"expand\" ";
		$src = base_url().'system/imports/img/min.gif';
		$ret = "<img src=\"$src\" $attr>";
		return $ret;
	}
	
	private function  max($pid){
		$attr = " style=\"cursor:pointer\" class=\"what\" param=\"$pid\" id=\"has_$pid\" state=\"collapse\" ";
		$src = base_url().'system/imports/img/max.gif';
		$ret = "<img src=\"$src\" $attr>";
		return $ret;
	}
	
	private function  this($pid){
		$attr = " style=\"cursor:pointer\" class=\"\" param=\"\" ";
		$src = base_url().'system/imports/img/this.gif';
		$ret = "<img src=\"$src\" $attr>";
		return $ret;
	}
	
	function scripts($ci){
		return $ci->tag->_script(array('jquery','jquery.validate','jquery.form','jquery-ui-min','jquery.jshowoff.min','jsfunctions'),'a');
	}
	function stylesheets($ci){
		return $ci->tag->_link(array('main','home','ui-lightness/jquery-ui-1.8rc3.custom'),'a');
	}
	
	
	function search(){
		return array(' ','&','/','(',')');
	}
	
	function replace(){
		return array('-',md5('&'),md5('/'),md5('('),md5(')'));
	}
	
	function clean_display($data){
		include_once(root_folder().'lib/function.lib.php');
		return cleanHTML($data);
	}
	
	
	
	
	
	function convert_anchor($str){
		$search = $this->search();
		$replace = $this->replace();
		return str_replace($search,$replace,$str);
	}
	
	function side_nav($aData,$indent='',$n=0){
		$ret = ''; 
		$a='';
		$r = 117+$n; 
		$g = 117+$n; 
		$b = 129+$n;
		$rgb = "rgb($r,$g,$b)";
		
		//$css = array('style'=>'display:block;padding:5px 5px 5px 5px ;background-color:'.$rgb.';border-bottom:solid #4b4b55; 1px;text-decoration:none;color:#FFFFFF;');
		$css = 'style="display:block;padding:5px 5px 5px 5px ;background-color:'.$rgb.';border-bottom:solid #4b4b55 1px;text-decoration:none;color:#FFFFFF;"';
		$acss = array('style'=>'text-decoration:none;color:#FFFFFF;'); 
		$ccss = 'style="display:none;"';
		if(count($aData)>0){
			foreach ($aData as $d){
				if(isset($d['child'])){
					$max = $this->max($d['id']);
					$ret .= "<span $css>$max&nbsp;".$indent.anchor('content/page/'.$this->convert_anchor($d['link_title']),$d['link_title'],$this->acss($d['id']))."</span>\n";
					$ret .= "<div id=\"\" class=\"cl_$d[id]\" $ccss >\n".$this->side_nav($d['child'],($indent.'--'),($n-30))."</div>\n";
				}else{
					$t = $this->this($d['id']);
					$ret .= "<span $css>$t&nbsp;&nbsp;".$indent.anchor('content/page/'.$this->convert_anchor($d['link_title']),$d['link_title'],$acss)."</span>\n";
				}
			}
		}
		return $ret;
	}
	
	private function acss($pid){
		return array('style'=>'text-decoration:none;color:#FFFFFF;','class'=>'overme','param'=>$pid); 
	}
	
	function f_news($ci_page, $table = 'tNews',$limit=null){
		$ret ='';
		$date = 'sNewsDate';
		$id = 'nNewsID';
		
		$res = $ci_page->get_record($table,array('nNewsID','title'),'',array($date=>'desc'),$limit)/*->result_array()*/;
		//$this->vdump($res->result_array());
		if($res->num_rows > 0){
			foreach ($res->result_array() as $d){
				$ret .= anchor('news/page/'.$d['nNewsID'],'<font style="margin-left:15px;">'.$d['title'].'</font>',array('class'=>'news_nav'));
			}
		}
		
		return $ret;
	}
	
	function main_menu($ci){
		
		return $arr = $ci->getRecords('pages',array('show_on_main_menu'=>'1','status'=>'active'),array('id','title','link_title'),array('weight'=>'asc'));
		
	}
	
	function feature_page($ci){
		
		return $arr = $ci->getRecords('pages',array('feature_page'=>'1','status'=>'active'),array('id','title','link_title','show_title','short_description'),array('weight'=>'asc'));
		
	}
	
	function footer_menu($ci_page){
		
		
		return $ci_page->getRecords('pages',array('show_on_footer_link'=>'1','status'=>'active'),array('id', 'title', 'link_title'),array('weight'=>'asc'));
		
	}
	
	function top_menu($ci_page){
		
		return $ci_page->getRecords('pages',array('show_on_top_links'=>'1','status'=>'active'),array('id','title','link_title'),array('top_link_weight'=>'asc'));
		
		
	}
	
	function main_content($title,$ci,$table){
		$res = $ci->page->getRecords($table,array('title'=>$title,'status'=>'active'));
	}
	
	function page_id($title,$ci,$index='title'){
		$res = $ci->page->getRecords('pages',array($index=>$title),array('id'));
		$res = $res->row_array();
		return $res['id'];
	}
	
	function get_page_by_id($title,$ci,$index='title'){
		return $res = $ci->page->getRecords('pages',array($index=>$title));
		 //$res = $res->row_array();
		//return $res['id'];
	}
	
	function child_links($parent,$ci){
		return $arr = $ci->getRecords('pages',array('status'=>'active','parent'=>$parent),array('id','title','link_title'),array('weight'=>'asc'));
	}
	
	function get_root_parent($ci,$pid){
		$ret = '';
		$res = reset($ci->get_record('pages','id, parent',array('id'=>$pid))->result_array());
		$ret = $res['id'];		
		if($res['parent'] != '0'){
			$res = reset($ci->get_record('pages','id, parent',array('id'=>$res['parent']))->result_array());
			$ret = $this->get_root_parent($ci,$res['id']);
		}
		
		return $ret;
	}
	
	function news_events($ci){
		return $ci->page->getRecords('news_events',array('status'=>'active'),array('id','title','link_title'));
	}
	
	function menu_tree($ci_page,$page_id=''){
		$res = array();
		if(empty($page_id)){
			$res = $ci_page->get_record('pages','id, title, link_title, parent, status',array('parent'=>'0','status'=>'active'),array('weight'=>'asc'))->result_array();
		}else{
			$res = $ci_page->get_record('pages','id, title, link_title, parent, status',array('parent'=>$page_id,'status'=>'active'),array('weight'=>'asc'))->result_array();
		}
		
		$size = count($res);
		for($i = 0; $i < $size; $i++){
			$res[$i];
			$temp = $this->menu_tree($ci_page,$res[$i]['id']);
			if(!empty($temp)){
				$res[$i]['child'] = $temp;	
			}
		}
		return $res;
	}
	
	function reverse_a_convert($page_id){
		$search = array(
			'-','6cff047854f19ac2aa52aac51bf3af4a',
			'6666cd76f96956469e7be39d750cc7d9',
			'84c40473414caf2ed4a7b1283e48bbf4',
			'9371d7a2e3ae86a00aab4771e39d255d'
		);
		$replace = array(' ','&','/','(',')');
		
		$page_id = str_replace($search,$replace,$page_id);
		
		return $page_id;
		
	}
	
	function display_news($ci_page,$id=''){
		$status = 1;
		
		$single = false;
		if(!empty($id)){
			if($id == 'Archive'){
				$id = '';
				$status = '0';
			}else{
				$single = true;	
				$status = NULL;
				
			}
			
		}
		$res = $ci_page->get_record('tNews','*',array('nNewsID'=>$id,'status'=>$status),array('sNewsDate'=>'acs'))->result_array();
		return $this->load->view('layout/news',array('news'=>$res,'single'=>$single,'status'=>$status),true);
	}
	
	function meta_tags($aData,$ci){
		return $ci->load->view('layout/meta_tags',array('content'=>$aData),true);
	}
	
	function get_inner_menu($ci_page, $page_id){
		$ret = '';
		$dl = '<div class="echo_b" style="width:500px;overflow:hidden;display:block;line-height:1.5em" ><ul>';
		$dr = '</ul></div>';
		return $this->create_link_tree($this->menu_tree($ci_page),NULL,$ret,$page_id,'','',1,'<br />');
	}
	
	function side_nav_($aData,$indent='',$n=0){
		$ret = ''; 
		$a='';
		$r = 117+$n; 
		$g = 117+$n; 
		$b = 129+$n;
		$rgb = "rgb($r,$g,$b)";
		
		$css = 'style="display:block;padding:5px 5px 5px 5px ;background-color:'.$rgb.';border-bottom:solid #4b4b55 1px;text-decoration:none;color:#FFFFFF;"';
		$acss = array('style'=>'text-decoration:none;color:#FFFFFF;'); 
		$ccss = 'style="display:none;"';
		if(count($aData)>0){
			foreach ($aData as $d){
				if(isset($d['child'])){
					$max = $this->max($d['id']);
					$ret .= "<span $css>$max&nbsp;".$indent.anchor('content/page/'.$this->convert_anchor($d['link_title']),$d['link_title'],$this->acss($d['id']))."</span>\n";
					$ret .= "<div id=\"\" class=\"cl_$d[id]\" $ccss >\n".$this->side_nav($d['child'],($indent.'--'),($n-30))."</div>\n";
				}else{
					$t = $this->this($d['id']);
					$ret .= "<span $css>$t&nbsp;&nbsp;".$indent.anchor('content/page/'.$this->convert_anchor($d['link_title']),$d['link_title'],$acss)."</span>\n";
				}
			}
		}
		return $ret;
	}
	
	function from_date($ci){
		$from_date = $ci->session->userdata($ci->from_date); 
		return '<input type="text" id="from_date" name="from_date" class="fromdate datepicker" value="'.$from_date.'" />';
	}
	
	function to_date($ci){
		$to_date = $ci->session->userdata($ci->to_date); 
		return '<input type="text" id="to_date" name="to_date" class="todate datepicker" value="'.$to_date.'" />';
	}
	
	function action_but($action){
		$ret = '<input type="hidden" id="action_c" name="action_c" value=""/>
		<input type="hidden" id="action_m" name="action_m" value="'.$action.'"/>';
		if($action == 'archived'):
			$ret .= '<input type="submit" value="Unarchive" param="unarchive" class="do_action"/>';
		else:
			$ret .= '<input type="submit" value="Archive" param="archive" class="do_action"/>';
		endif;
		
		$ret .= '<input type="submit" value="Delete" param="delete"  class="do_action"/>
		 <input type="button" value="Select All" class="do_select_all">
		 <input type="button" value="Select None" class="do_select_none">';
		return $ret;
	}
	
	function do_archive($ci,$cond=true){
		if(!$ci->input->post('cbox'))
			exit();
		$set = 'archived';
		if(!$cond){
			$set = 'active';	
		}
		foreach ($ci->input->post('cbox') as $d){
			$data = array('status'=>$set);
			$ci->db->where($ci->uid, $d);
			$ci->db->update($ci->tablename, $data); 
			$ci->db->flush_cache();
		}
		
	}
	
	function do_delete($ci){
		if(!$ci->input->post('cbox'))
			exit();
			
		foreach ($ci->input->post('cbox') as $d){
			$this->delete_files($ci,$d);
			$ci->db->where($ci->uid, $d);
			$ci->db->delete($ci->tablename); 
			$ci->db->flush_cache();
		}
		
	}
	
	function delete_files($ci,$id){
		if($ci->tablename == 'images' || $ci->tablename == 'files'){
			$res = $ci->page->getRecords($ci->tablename,array($ci->uid=>$id),array('userfile'))->row_array();
			$userfile = $res['userfile'];
			
			if($userfile){
			
				unlink(root_folder().'/uploads/'.$userfile);
				switch ($ci->tablename){
					case 'images':
						if(file_exists(root_folder().'/thumbnails/'.$userfile)){
							unlink(root_folder().'/thumbnails/'.$userfile);
						}
						
						break;
					default:
						break;		
				}		
			}
			
		}
	}
	
	function replace_display($ci,$records){
		$sz = count($records);
		for($i = 0; $i < $sz; $i++){
			$ci->sum_hours += '';
		}
		return $records;
	}
	
	
	function pagination($ci){
		
		$page = 0;
		$limit = 15;
		$post_page = $ci->input->post('page');
		$post_limit = $ci->input->post('limit');
		if($post_page || $page == 0){
			$page = $post_page;
		}
		if($post_limit){
			$limit = $post_limit;
		}
		$where = NULL;//array('TransitionID'=>0);
		$param = array(
			'total_record'=>array('table'=>$ci->tablename,'where'=>$ci->where),
			'page'=>$page,
			'limit'=>$limit,
			'order'=>$ci->input->post('order'),
			'action'=> $ci->controller.'/action/'.$ci->pagin_action,
			'status'=>$ci->pagin_action
		);
		$ci->pagin->set_param($param);
		
		return $ci->load->view('test/pagination_controls',$ci->pagin,true);
		
	}
	
	function upload_data($ci,$type='pictures'){
		
		if(!empty($_FILES['userfile']['name'])){
        	$r = $ci->page->getRecords('all_file_type',array('type'=>$type))->row_array();
			$config = array(
				'upload_path'=>root_folder().'/uploads/',
				'allowed_types'=>$r['allow'],
				'file_name'=>uniqid(),
			);
	
			$ci->load->library('upload', $config);
			
			$res = $ci->upload->do_upload();
        
	        if (!$res){
				$error = array('error' => $ci->upload->display_errors());
				$ci->msg = $error['error'];
				
			}else{
				
				$data = array('upload_data' => $ci->upload->data());
				
				$upload_data = $data['upload_data'];
				
				if($type=='pictures'){
					
					unset($config);
				
					$config = array(
						'image_library'=>'gd2',
						'source_image'=>$upload_data['full_path'],
						'create_thumb'=>TRUE,
						'width'=>($upload_data['image_width'] * .15),
						'height'=>($upload_data['image_height'] * .15),
						'thumb_marker'=>'',
						'new_image'=>root_folder().'/thumbnails/',
					);
					
					$ci->load->library('image_lib', $config); 
					
					$ci->image_lib->resize();
				}
			}
        }
        
		$id = $ci->input->post('uid');
		
		if(isset($upload_data['file_name'])){
			$ci->data['userfile'] = $upload_data['file_name'];
			if($type='files'){
				$ci->data['file_type'] = $this->get_file_extention($upload_data['file_name']); 
			}
			if(!empty($id)){
				$res = reset($ci->page->get_record($ci->tablename,'userfile',array($ci->uid=>$id))->result_array());
				if($res['userfile']){
					if(file_exists(root_folder().'thumbnails/'.$res['userfile'])&&$type=='pictures'){
						unlink(root_folder().'thumbnails/'.$res['userfile']);
					}
					if(file_exists(root_folder().'uploads/'.$res['userfile'])){
						unlink(root_folder().'uploads/'.$res['userfile']);	
					}
				}
			}
		}else{
			unset($ci->data['userfile']);
		}
	}
	
	function get_file_extention($filename){
		$arr = explode('.',$filename);
		return $arr[count($arr)-1];
	}
	
	function is_image($filename){
		$imglist = array('gif','png','bmp','jpg');
		$ext = $this->get_file_extention($filename);
		$ret = false;
		if(in_array($ext,$imglist)){
			$ret = true;
		}
		return $ret;
	}
	
	function action_list($ci){
		$ret = '';
		
		$ret[] = array(
			'uri'=>$ci->module.'/action/edit/',
			'title'=>'<img src="'.base_url().'system/imports/img/ico_edit.gif">',
			'attributes'=>array('title'=>'Edit')
		);
		
		if(isset($ci->extra_action)&&is_array($ci->extra_action)){
			$ret[]=$ci->extra_action;
		}
		return $ret;
	}
	
	function common_index($ci){
		
		if($ci->actionv){
			$ci->pagin_action = $ci->actionv;
			
		}else{
			$ci->pagin_action = $ci->input->post('list_status');
		}
		
		
		$aParam = array('base'=>base_url());
		if($ci->uidv == 'NULL')
			$ci->uidv = NULL;
			
		$ci->tpl->init_param('stylesheets',$ci->tag->_link(array('main','home','ui-lightness/jquery-ui-1.8rc3.custom'),'a'));
		$ci->tpl->init_param('menu_tabs',$ci->load->view('layout/menu_tabs','',true));
		$ci->tpl->init_param('contents',$ci->load->view('layout/contents','',true));
		$ci->tpl->init_param('scripts',$this->scripts($ci));
		$ci->tpl->init_param('admin_menu',$this->admin_menu($ci));
		
		$filt = $ci->input->post('filt');
		echo $ci->input->post('action_m');
		if(!empty($filt[0])){
			$ci->uidv = $filt[0];
			$ci->actionv = $ci->input->post('action_m');
		}
		
	}
	
	
	function admin_menu($ci){
		return 
		'	<ul id="jsddm">
				
				<li><a href="#">Article</a>
					<ul>
						'.(($this->has_privilege($ci,'list','manager_pages'))? '<li>'. anchor('manager_pages','Pages').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_categories'))? '<li>'. anchor('manager_categories','Categories').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_news_events'))? '<li>'. anchor('manager_news_events','News & Events').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_properties'))? '<li>'. anchor('manager_properties','Properties').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_contacts'))? '<li>'. anchor('manager_contacts','Contacts').'</li>' : '' ).'
					</ul>
				</li>
				<li><a href="#">Uploads</a>
					<ul>
						'.(($this->has_privilege($ci,'list','manager_mutiple_upload'))? '<li>'. anchor('manager_mutiple_upload','Multiple Upload').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_pictures'))? '<li>'. anchor('manager_pictures','Pictures').'</li>' : '' ).'
						'.(($this->has_privilege($ci,'list','manager_files'))? '<li>'. anchor('manager_files','Files').'</li>' : '' ).'
					</ul>
				</li>
				<!--<li><a href="#">My Desk</a>
					<ul>
						<li><a href="#">Change Username/Password</a></li>
					</ul>
				</li>-->
			</ul>
		';
	}
	
	function renderlist($uidv,$action,$ci){
		
		$ci->page->rewrite_image_list_js();
		
		switch ($action){
			case 'archived':
				$param['sidebars'] = array('List'=>$ci->module.'/action/list','Archived'=>'');
				$param['title'] = $ci->module_name.' Archived';
				if(isset($ci->archived_title)){
					$param['title'] = $ci->archived_title;
				}
				
				$param['action'] = 'archived';
				
				$ci->where = $ci->archives;
				break;
			default:
				$param['sidebars'] = array('List'=>'','Archived'=>$ci->module.'/action/archived');
				$param['title'] = 'List '.$ci->module_name;
				$param['title'] = 'List '.$ci->module_name;
				if(isset($ci->list_title)){
					$param['title'] = $ci->list_title;
				}
				$param['action'] = 'list';
				$ci->where = $ci->actives;
				break;	
		}
		
		$tables = array($ci->tablename);
		$fields = array('*');
		$join = '';
		if(isset($ci->fields)){
			$fields = $ci->fields;
		}
		if(isset($ci->join)){
			$join = $ci->join;
		}
		/*if(isset($ci->table)){
			$tables = $ci->table;
		}*/
		if($ci->input->post('key_field') && $ci->input->post('order')){
			$ci->sort = array($ci->input->post('key_field')=>$ci->input->post('order'));
		}
		
		$param['pagination'] = $this->pagination($ci);
		$limit = array('limit'=>$ci->pagin->limit,'offset'=>$ci->pagin->offset);
		
		$res = $ci->page->getRecords($tables,$ci->where,$fields,$ci->sort,$limit,$join);
		$param['records'] = $this->init_inline_edit($this->replace_display($ci,$res->result_array()),$ci);
		//$this->vdump($param['records']);
		
		//p($res->result_array(),true);
		
		$param['action_list'] = $this->action_list($ci);
		$param['cbox'] = true;
		$param['message'] = '<font style="color:red;">'.$ci->msg.'</font>';
		$param['action_but'] = $this->action_but($param['action']);
		$param['fiters'] = '';
		$param['footer'] = '';
		$param['commonjs'] = $ci->tag->_script(array('common'),'a');
		$param['image_dir'] = 'thumbnails';
		$param['header'] = $ci->header_list;
		$param['mine'] = $ci;
		
		
		return $ci->load->view('test/listing',$param,true);
	}
	
	function init_inline_edit($aData,$ci){
//		<label class="editable" index="2">Ramon</label>
//		<input type="text" class="data" index="2" value="Ramon" name="description" table_from="images" style="display:none;">
		$newData = array();
		$not_included = array('userfile','id','thumbnail','status','type','file_type','company_name');
		
		foreach ($aData as $d){
			$index=$d['id'];
			foreach ($d as $i=>$v){
				if(!in_array($i,$not_included)){
					$display = $v;
					if(!$v){
						$display = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
					}
					$d[$i] = "<label class=\"editable\" index=\"{$index}\" pointer=\"{$i}_{$index}\">{$display}</label>
							<input type=\"text\" size=\"30\" class=\"data_editable\" index=\"{$index}\" pointer=\"{$i}_{$index}\" value=\"{$v}\" name=\"{$i}\" table_from=\"{$ci->tablename}\" style=\"display:none;\">";
				}
			}
			$newData[] = $d;
		}
		
		//$this->vdump($newData);
		
		return $newData;
	}
	
	function has_privilege($ci,$action='list',$menu=NULL){
		$ret = false;
		$controller = $ci->controller;
		
		if($menu!=NULL){
			$controller = $menu;
		}
		
		
		//$privileges = $ci->session->userdata('privileges');
		$res = $ci->page->getRecords('user_groups',array('id'=>$this->user('UserGroup')));
		
		if($res->num_rows>0){
			$res = $res->row_array();
			$privileges = $arr['privileges'] = json_decode($res['privileges']);
			
			if($action=='list'){
				$pattern = '/^'.$controller.'/';
			}else{
				$pattern = '/^'.$controller.'\/action\/'.$ci->action.'/';
			}
			
			foreach ($privileges as $d){
				if(preg_match($pattern, $d)){
					$ret = true;
					break;
				}
			}	
			
		}
		
		
		
		if(!$ret && $menu==NULL){
			header("Location: ".base_url().'index.php/noaccess');
		}
		
		return $ret;
	}
	
	function is_admin($ci){
		$ret = false;
		
		$UserGroup = $this->user('UserGroup');
		
		$vars = array(
			'table'=>'user_groups a',
			'like'=>array(array('a.name','admin','both')),
			'where'=>array('a.id'=>$UserGroup)
		);
		
		$res = $ci->page->basicSelect($vars);
		if($res->num_rows>0)
			$ret = true;
			 
		return $ret;
	}
	
	function is_logged($ci){
		$ret = true;
		$user = $this->user('id');
		if(!$user){
			$ret = true;
		}
		return $ret;
	}
	
	function auto_login($ci,$controller='content'){
		$rememberid = get_cookie('ci_userid');
		$user = $this->user('id');
		if($rememberid && !($user)){
			$res = $ci->page->get_record('users','*',array('id'=>$rememberid))->row_array();
			$arr['user'] = $res;
			$privileges = $ci->page->getRecords('user_groups',array('id'=>$res['UserGroup']))->row_array();
			$arr['privileges'] = json_decode($privileges['privileges']);
			$ci->session->set_userdata($arr);
			header("Location: ".base_url()."index.php/".$controller);
			exit();
		}
		
	}
	
	function log_or_not($ci){
		$user = $this->user('id');
		$ret = true;
		if(!$user){
			$ret = false;
		}
		return $ret;
	}
	
	function jquery(){
		return base_url().'system/imports/js/jquery.js';
	}
	
	function log_visitor($db){
		$db->insert('visitors',array('ip_address'=>$_SERVER['REMOTE_ADDR'],'date_visited'=>date('Y-m-d H:i')));
	}
	
	
	function multiple_upload($ci){
		return $ci->load->view('test/multiple_upload',array('userid'=>$ci->userid()),true);
	}
	
	function mce_init($ci){
		return $ci->load->view('crud/mce_init',NULL,true);
	}
	
	
	
	
}